package com.health.controller;

import com.health.common.constant.MessageConstant;
import com.health.common.entity.CheckGroup;
import com.health.common.entity.SetMeal;
import com.health.common.vo.QueryPageBean;
import com.health.common.vo.RespBean;
import com.health.common.vo.RespPageBean;
import com.health.feignclient.CheckItemAndGroupClient;
import com.health.service.SetMealService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CheckItemAndGroupClient checkItemAndGroupClient;

    @GetMapping("/")
    public RespPageBean getAllSetMealByPage(QueryPageBean queryPageBean){
        return setMealService.getAllSetMealByPage(queryPageBean);
    }

    @GetMapping("/group/{id}")
    public List<CheckGroup> getCheckGroupBySetMealId(@PathVariable Integer id){
        return checkItemAndGroupClient.getGroup4SetMeal(id);
    }

    @GetMapping("/group/")
    public List<CheckGroup> getAllCheckGroup(){
        return checkItemAndGroupClient.getAllCheckGroup4SetMeal();
    }

    @GetMapping("/getGroupIdsById/{id}")
    public List<Integer> getGroupIdsById(@PathVariable Integer id){
        return setMealService.getGroupIdsById(id);
    }

    @PostMapping("/upload")
    @Transactional
    public RespBean uploadImg(@RequestParam("imgFile")MultipartFile imgFile) throws IOException {
//        String oldFileName = imgFile.getOriginalFilename();
        String extension = "."+ FilenameUtils.getExtension(imgFile.getOriginalFilename());
        String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+ UUID.randomUUID().toString().replace("-","")+extension;

        String realPath = ResourceUtils.getURL("classpath:").getPath()+"/static/img";

        imgFile.transferTo(new File(realPath,newFileName));

        RespBean respBean = RespBean.build();
        respBean.setMessage("上传成功");
        respBean.setStatus(200);
        respBean.setObj(newFileName);
        return respBean;
    }

    @PostMapping("/")
    public RespBean addSetMeal( SetMeal setMeal,@RequestParam("items") Integer[] items){
//        System.out.println("setMeal="+setMeal.toString());
//        System.out.println("items="+ Arrays.toString(items));
        if(setMealService.addSetMeal(setMeal,items) == 1){
            return RespBean.ok(MessageConstant.ADD_SETMEAL_SUCCESS);
        }
        return RespBean.error(MessageConstant.ADD_SETMEAL_FAIL);
    }

    @PutMapping("/")
    public RespBean updateSetMeal(SetMeal setMeal,@RequestParam("items")Integer[] items){
//        System.out.println("setMeal="+setMeal.toString());
//        System.out.println("items="+ Arrays.toString(items));
        if(setMealService.updateSetMeal(setMeal,items) == 1){
            return RespBean.ok("更新套餐成功");
        }
        return RespBean.error("更新套餐失败");
    }

    @DeleteMapping("/{id}")
    public RespBean deleteSetMealById(@PathVariable Integer id){
        if(setMealService.deleteSetMealById(id)==1){
            return RespBean.ok("删除套餐成功");
        }
        return RespBean.error("删除套餐失败");
    }

}
